package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdRoleModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdSubOfferModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProductModuleVO;
import com.iwhalecloud.bss.kite.dataservice.cache.AdditionalExtProdCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.AdditionalExtProd;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <Description>套餐-可选包组件<br>
 *
 * @author zhu.zixin<br>
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.handler.module <br>
 * @since R9.0<br>
 */
@Service
public class LocalAdditionalOfferModuleHandler extends LocalAbstractAdditionalOfferModuleHandler {

    @Override
    public List<ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        return Collections.singletonList(getViewSceneVo(context, sceneModule));
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        return Collections.singletonList(getViewSceneVo(context, sceneModule));
    }

    private ProdSubOfferModuleVO getViewSceneVo(AcceptContext context, SceneModule sceneModule) {
        // 从父模块里面获取到产品组件对象
        ProductModuleVO productVO = (ProductModuleVO) sceneModule.getParentModule().getParentModuleVO();
        ProdRoleModuleVO prodRole = (ProdRoleModuleVO) sceneModule.getParentModuleVO();

        List<String> mustOfferList = new ArrayList<>();

        if (prodRole.isAdditionalOfferAdd()) {
            //可选包加装
            List<AdditionalExtProd> additionalExtProds = AdditionalExtProdCache.getInst()
                .getAdditionalOffer(context.getAcceptRequest().getOfferId(), productVO.getProdId(), prodRole.getProdRoleId());
            additionalExtProds.forEach(additionalExtProd -> mustOfferList.add(additionalExtProd.getOfferId()));
        }

        ProdSubOfferModuleVO prodSubOfferModuleVO = getAdditionalOfferModuleVO(
            listAdditionalOfferInstsByProdInstId(context.getAcceptRequest().getSceneInstId(), prodRole.getProdInstId(),
                context.getAcceptRequest().getCustId()), context.getAcceptRequest().getOfferId(), productVO.getProdId(), prodRole.getProdRoleId(),
            mustOfferList);

        prodRole.setProdSubOffer(prodSubOfferModuleVO);
        return prodSubOfferModuleVO;
    }

}
